clear frames 

** Here we go from the compiled dataset and add/modify variables
use "data/ready4wrangling.dta", clear
keep if dataset!=""

********************************************************************************
****** DEMOGRAPHICS AND SOCIOECONOMIC BACKGROUND FROM WAVE DATA ****************
********************************************************************************

*** Age (this ensures that all respondents with same birthy have same age)
gen age=year-birthy if birthy>0
	order age, before(age_dv)

recode birthy (1935/1944=1) (1945/1958=2) (1959/1976=3) (nonm=.), gen(polgen)
	
* Sex
recode sex (1=0) (2=1) (nonm=.), gen(female)
	order female, before(sex)
	drop sex
	
* Education 
gen education=1 if inlist(isced,1,2)
replace education=2 if inlist(isced,3,4)
replace education=3 if inlist(isced,5,6,7)
// based on www.lisdatacenter.org/wp-content/uploads/standardisation-of-education-levels.pdf	
label variable education "Current level of education"
label define education 1 "Low Edu" 2 "Med Edu" 3 "Hgh Edu", modify
label val *education education
order education, after(isced)
drop isced 

* Highest education
bys pidp: egen maxeduc=max(education)
label var maxeduc "Highest level of education"
label values maxeduc education
	order maxeduc, after(education)
	
********************************************************************************
****** ATTITUDES AND VOTING BEHAVIOR
********************************************************************************

recode *opsoc* (min/0=.)
alpha opsoca opsocb opsocc opsocd opsoce opsocf if !inlist(year,1998,2008), gen(red) reverse(opsocb opsocd opsoce opsocf) item detail c

egen redz = std(red)
label var redz "Pro-Redistribution Scale"

gen opsocbR = 6-opsocb 
gen opsocdR = 6-opsocd 
gen opsoceR = 6-opsoce
gen opsocfR = 6-opsocf 

egen reda = std(opsoca)
egen redb = std(opsocbR)
egen redc = std(opsocc)
egen redd = std(opsocdR)
egen rede = std(opsoceR)
egen redf = std(opsocfR)

label var reda "Ordinary people share nations wealth"
label var redb "One law for rich, one for poor"
label var redc "Private enterprises solve economic probs"
label var redd "Public services ought to be state owned"
label var rede "Govt has obligation to provide jobs"
label var redf "Strong trade unions protect employees"

********************************************************************************
****** CREATE LONG-TERM CLASS MEMBERSHIP VARIABLES
********************************************************************************

merge 1:1 pidp wave using "data/wave_classhistory", nogen
	
	* Drop all observations before first and after last observed red pref 
	bys pidp (year): egen temp=min(year) if !mi(red)
	bys pidp (year): egen min_year_red=min(temp)
	drop temp
	bys pidp (year): egen temp=max(year) if !mi(red)
	bys pidp (year): egen max_year_red=max(temp)
	drop temp
	
	keep if inrange(year,min_year_red,max_year_red)
	
	bys pidp (year): egen N_red=total(!missing(red))  // Total number of red obs
	drop if N_red==0 

	* Create counter for red obs
	preserve 
		keep if !mi(red)
		bys pidp (year): gen n_red= _n
		tempfile t
		save `t'
	restore 

merge m:1 pidp year using `t', nogen keep(match master)

********************************************************************************
****** PANEL STABLE CLASS
********************************************************************************

*Requirements:
	* Observed in employment for at least 8 waves
	* X% share of time in same class

tabulate egp_indresp,gen(egp_indresp)
				
	foreach n in 1 2 3 4 5 {
		// Cumulative number of waves in class
		bys pidp (waven): gen egp`n'waves = sum(egp_indresp`n') 
	}

	recode egp_indresp (50/max=.), gen(egp_indresp_emp)	
	
	bys pidp (year): egen N_egp_indresp=total(!missing(egp_indresp))  // Total number of employment status obs
	bys pidp (year): egen N_egp_indresp_emp=total(!missing(egp_indresp_emp))  // Total number of class obs
	bys pidp (year): egen N_red_egp_indresp=total(!mi(egp_indresp)&!mi(red))  // Total number of red+employment status obs
	bys pidp (year): egen N_red_egp_indresp_emp=total(!mi(egp_indresp_emp)&!mi(red))  // Total number of red+class obs
		
	* Create counter for class obs
	preserve 
		keep if !mi(egp_indresp)
		bys pidp (year): gen n_egp_indresp= _n
		tempfile t
		save `t'
	restore 
	
	merge m:1 pidp year using `t', nogen keep(match master)

	preserve 
		keep if !mi(egp_indresp_emp)
		bys pidp (year): gen n_egp_indresp_emp= _n
		tempfile t
		save `t'
	restore 

	merge m:1 pidp year using `t', nogen keep(match master)

	preserve 
		keep if !mi(egp_indresp) & !mi(red)
		bys pidp (year): gen n_red_egp_indresp= _n
		tempfile t
		save `t'
	restore 

	merge m:1 pidp year using `t', nogen keep(match master)

	preserve 
		keep if !mi(egp_indresp_emp) & !mi(red)
		bys pidp (year): gen n_red_egp_indresp_emp= _n
		tempfile t
		save `t'
	restore 

	merge m:1 pidp year using `t', nogen keep(match master)
	
* Create variable
	preserve 
		bys pidp (year): keep if _n == _N // Only keep last obs
		tab N_red if inrange(N_red,5,7)
		tab N_red if inrange(N_red,5,7) & inrange(N_egp_indresp_emp,8,18)
		
		keep if inrange(N_egp_indresp_emp,8,18) // Only keep cases with more than 8 employment obs
		
		egen maxegp_waves=rowmax(egp?waves) 
		egen sumegp_waves=rowtotal(egp?waves)
		gen max_egp_class=.
			replace max_egp_class=1 if maxegp_waves==egp1waves
			replace max_egp_class=2 if maxegp_waves==egp2waves
			replace max_egp_class=3 if maxegp_waves==egp3waves
			replace max_egp_class=4 if maxegp_waves==egp4waves
			replace max_egp_class=5 if maxegp_waves==egp5waves
		gen max_egp_time=maxegp_waves/sumegp_waves
				
			foreach t in 75 90  {
				local threshold=`t'/100
				* Panel stable, overall
				gen egp_panel`t'=max_egp_class if inrange(max_egp_time,`threshold',1) 
				replace egp_panel`t'=120 if inrange(max_egp_time,0,`threshold') & mi(egp_panel`t')
					
			}
		tab egp_panel75 if inrange(N_red,5,7) & inrange(N_egp_indresp_emp,8,18), m
		keep pidp maxegp_waves max_egp_class max_egp_time egp_panel*
	tempfile t
	save `t'
	
	restore 
	
	merge m:1 pidp using `t', nogen
	
	
label val egp_panel75 egp						  
label val egp_panel90 egp
	
* Create more restrictive variable, such that individuals only start to belong to their stable class
* after 8 periods in employment AND over 75% in same class.
        
        * Current share of time in eventual stable class
                bysort pid (year): carryforward n_egp_indresp_emp, gen(n_curr)
                gen panelclass_curr=.
                replace panelclass_curr = egp1waves/n_curr if egp_panel75==1
                replace panelclass_curr = egp2waves/n_curr if egp_panel75==2
                replace panelclass_curr = egp3waves/n_curr if egp_panel75==3
                replace panelclass_curr = egp4waves/n_curr if egp_panel75==4
                replace panelclass_curr = egp5waves/n_curr if egp_panel75==5
        
                clonevar egp_panel75res=egp_panel75
                replace egp_panel75res=. if mi(panelclass_curr) | mi(n_curr) | panelclass_curr<0.75 | n_curr<8
                replace egp_panel75res=120 if egp_panel75==120 & n_curr>7 & !mi(n_curr)
                label variable egp_panel75res "Restrictive version of panel variable"
				
********************************************************************************
* CREATE LIFETIME STABLE CLASS VARIABLE 	
********************************************************************************

preserve 
		bys pidp (year): keep if _n == _N // Only keep last obs
		
		egen maxegpmonths=rowmax(egp1months egp2months egp3months egp4months egp5months) 
		egen sumegpmonths=rowtotal(egp1months egp2months egp3months egp4months egp5months) 
		gen max_egp_lifeclass=.
			replace max_egp_lifeclass=1 if maxegpmonths==egp1months
			replace max_egp_lifeclass=2 if maxegpmonths==egp2months
			replace max_egp_lifeclass=3 if maxegpmonths==egp3months
			replace max_egp_lifeclass=4 if maxegpmonths==egp4months
			replace max_egp_lifeclass=5 if maxegpmonths==egp5months
		gen max_egp_lifetime=maxegpmonths/sumegpmonths
				
		foreach t in 75 90  {
				local threshold=`t'/100
				* Lifetime stable, overall
				gen egp_lifetime`t'=max_egp_lifeclass if inrange(max_egp_lifetime,`threshold',1) & inrange(egp_nummonths,96,2000)
				replace egp_lifetime`t'=120 if inrange(max_egp_lifetime,0,`threshold') & inrange(egp_nummonths,96,2000) & mi(egp_lifetime`t')
					 replace egp_lifetime`t'=max_egp_lifeclass if inrange(max_egp_lifetime,`threshold',1) & inrange(egp_nummonths,36,95) & age<30 & mi(egp_lifetime`t')
					 replace egp_lifetime`t'=120 if inrange(max_egp_lifetime,0,`threshold') & inrange(egp_nummonths,36,95) & age<30 & mi(egp_lifetime`t')
					
		}
		
		keep pidp egp_lifetime*
	tempfile t
	save `t'
	
restore 
	
merge m:1 pidp using `t', nogen

label val egp_lifetime75 egp						  
label val egp_lifetime90 egp

* Create more restrictive variable, such that individuals only start to belong to their stable class
* after 8 periods in employment AND over 75% in same class.
        
        * Current share of time in eventual stable class
                gen lifetimeclass_curr=.
                replace lifetimeclass_curr = egp1months/egp_nummonths if egp_lifetime75==1
                replace lifetimeclass_curr = egp2months/egp_nummonths if egp_lifetime75==2
                replace lifetimeclass_curr = egp3months/egp_nummonths if egp_lifetime75==3
                replace lifetimeclass_curr = egp4months/egp_nummonths if egp_lifetime75==4
                replace lifetimeclass_curr = egp5months/egp_nummonths if egp_lifetime75==5
        
                clonevar egp_lifetime75res=egp_lifetime75
                replace egp_lifetime75res=. if mi(lifetimeclass_curr) | mi(egp_nummonths) | lifetimeclass_curr<0.75 | egp_nummonths<96
                replace egp_lifetime75res=120 if egp_lifetime75==120 & egp_nummonths>95 & !mi(egp_nummonths)
                label variable egp_lifetime75res "Restrictive version of lifetime variable"
                
				
********************************************************************************
****** CREATE CLASS MOBILITY VARIABLES FOR COMBINED ANALYSIS
********************************************************************************

* Use only concurrent data from individual response files. 
* Define at-risk sample and find respondents who are "permanently" mobile

cap drop mobility
	cap frame drop mobility
	frame copy default mobility
	frame change mobility

	xtset pidp waven

	keep if inrange(egp_indresp, 1,4) 
	bys pidp (waven): gen egp_indresp_1 =  egp_indresp[1]
	
	* Find class transitions
	gen egp_L = l.egp_indresp 
	gen egp_L2 = l2.egp_indresp
	gen egp_L3 = l3.egp_indresp
	gen egp_L4 = l4.egp_indresp
	gen egp_L5 = l5.egp_indresp
	gen egp_L6 = l6.egp_indresp
	
	gen egp_diff = egp_indresp-egp_L if inrange(egp_indresp,1,4) & inrange(l.egp_indresp,1,4)
	replace egp_diff = egp_indresp-egp_L2 if mi(egp_diff) & inrange(egp_indresp,1,4) & inrange(l2.egp_indresp,1,4)
	replace egp_diff = egp_indresp-egp_L3 if mi(egp_diff) & inrange(egp_indresp,1,4) & inrange(l3.egp_indresp,1,4)
	replace egp_diff = egp_indresp-egp_L4 if mi(egp_diff) & inrange(egp_indresp,1,4) & inrange(l4.egp_indresp,1,4)
	replace egp_diff = egp_indresp-egp_L5 if mi(egp_diff) & inrange(egp_indresp,1,4) & inrange(l5.egp_indresp,1,4)
	replace egp_diff = egp_indresp-egp_L6 if mi(egp_diff) & inrange(egp_indresp,1,4) & inrange(l6.egp_indresp,1,4)
	
	* Any time up/downwardly mobile?
	bys pidp (waven): egen egp_anytime_down=min(egp_diff)
	bys pidp (waven): egen egp_anytime_up=max(egp_diff)

	* Up/downwardly mobile up to this obs?
	gen temp1=1 if egp_diff>0 & !mi(egp_diff)
	bys pidp (waven): gen egp_previously_up=sum(temp1)
	drop temp1
	
	gen temp1=1 if egp_diff<0 
	bys pidp (waven): gen egp_previously_down=sum(temp1)
	drop temp1 
	
	* At risk Only those in classes 1 to 3 (up) and 2 to 4 (down) in first obs are at risk of being upwardly or downwardly mobile, respectively
	* They are at risk until they are no longer observed in any of the first four classes or transition in the other direction
	gen atrisk_up=0
		replace atrisk_up=1 if inrange(egp_indresp_1,1,3)   ///
												  & inrange(egp_indresp,1,4) ///
												  & egp_previously_down==0
		
	gen atrisk_down=0
		replace atrisk_down=1 if inrange(egp_indresp_1,2,4)  ///
												  & inrange(egp_indresp,1,4) ///
												  & egp_previously_up==0

    gen atrisk=0
		replace atrisk=1 if atrisk_up==1 | atrisk_down==1

	* When was the first occasion on which they were upwardly mobile?
	by pidp (waven), sort: gen byte firstup = sum(inrange(egp_diff, 1,3)) == 1  & sum(inrange(egp_diff[_n - 1],1,3)) == 0  
		gen N_up=waven if firstup==1 
		bys pidp (waven): egen egp_n_up=min(N_up) 
		drop N_up
	
	* When was the first occasion on which they were downwardly mobile?
	by pidp (waven), sort: gen byte firstdown = sum(inrange(egp_diff, -3,-1)) == 1  & sum(inrange(egp_diff[_n - 1],-3,-1)) == 0 
		gen N_down=waven if firstdown==1 
		bys pidp (waven): egen egp_n_down=min(N_down)
		drop  N_down

	* When was the first occasion they were mobile in either direction?
	gen egp_n_either=.
		replace egp_n_either=egp_n_up if mi(egp_n_down)
		replace egp_n_either=egp_n_down if mi(egp_n_up)
		replace egp_n_either=egp_n_up if egp_n_up<egp_n_down & !mi(egp_n_up) & !mi(egp_n_down)
		replace egp_n_either=egp_n_down if egp_n_up>egp_n_down & !mi(egp_n_up) & !mi(egp_n_down)
	
	* Create treatment variable for mobility state 
	* Treatment: Being a class member and then transitioning to being a "permanent" class member of higher/lower class
	* Note that the atrisk_up==1 requirement ensures that once an upwardly mobile R moves down, he is no longer upwardly mobile
	gen egp_up0=0 if atrisk_up==1 
		replace egp_up0=1 if egp_previously_up!=0 & atrisk_up==1 & l.atrisk_up==1 
		
	by pidp (waven): egen egp_up0_treated=max(egp_up) if !mi(egp_up)

	gen egp_down0=0 if atrisk_down==1
		replace egp_down0=1 if egp_previously_down!=0 & atrisk_down==1 & l.atrisk_down==1
	by pidp (waven): egen egp_down0_treated=max(egp_down) if !mi(egp_down)
	
	gen treat=0 if atrisk==1
		replace treat=1 if egp_down0==1
		replace treat=2 if egp_up0==1
	    label variable treat "Current (permanent) mobility status"
		label define treat 0 "Immobile" 1 "Downwardly mobile" 2 "Upwardly mobile"
		label values treat egp_treat 
	
	by pidp (waven): egen egp_treated=max(treat) if !mi(egp_down0) | !mi(egp_up0)
		recode egp_treated (1 2 =1)
	    label variable egp_treated "(Permanent) mobility anytime during the sample period"
		label define egp_treated 0 "Immobile" 1 "Downwardly mobile" 2 "Upwardly mobile"
		label values egp_treated egp_treated 
		
	* Create variable for number of years since mobility
	gen egp_yrs=-1 if treat==0 
		replace egp_yrs= waven-egp_n_either if inrange(treat,1,2)
		label variable egp_yrs "Years since mobility event"
		label define egp_yrs -1 "Never mobile" 0 "Year of mobility"
		label values egp_yrs egp_yrs
		
		
frame change default
frlink 1:1 pid waven, frame(mobility)
frget egp_treated treat egp_yrs, from(mobility)
	
drop mobility 
frame drop mobility

keeporder pidp pid hid year waven age birthy polgen female education education maxeduc ///
		p_egp egp egp_indresp N_egp_indresp n_egp_indresp N_egp_indresp_emp n_egp_indresp_emp  ///
		egp_panel* egp_lifetime* egp_treated treat egp_yrs ///
		red? N_red n_red N_red_egp_indresp n_red_egp_indresp N_red_egp_indresp_emp n_red_egp_indresp_emp 

save "data/ready4analysis.dta", replace	
	
